<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta name="referrer" content="same-origin">
<meta name="referrer" content="no-referrer" />








<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Docker," />










<meta name="description" content="&amp;emsp;&amp;emsp;Docker是Docker.Inc开发的一款开源容器引擎，它在容器的基础上，将文件、网络、进程等一系列资源进行进一步封装和管理，通过对镜像的打包和封装，以及引入Docker Registry集中存储Docker镜像，从而简化了整个开发、测试和部署的环境和流程。">
<meta name="keywords" content="Docker">
<meta property="og:type" content="article">
<meta property="og:title" content="拥抱Docker（一）常用命令">
<meta property="og:url" content="http://yoursite.com/2019/05/27/拥抱Docker（一）常用命令/index.html">
<meta property="og:site_name" content="矩阵编程">
<meta property="og:description" content="&amp;emsp;&amp;emsp;Docker是Docker.Inc开发的一款开源容器引擎，它在容器的基础上，将文件、网络、进程等一系列资源进行进一步封装和管理，通过对镜像的打包和封装，以及引入Docker Registry集中存储Docker镜像，从而简化了整个开发、测试和部署的环境和流程。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/639cfad5gy1g3erlk4ez1j20xk0eon4e.jpg">
<meta property="og:updated_time" content="2019-10-24T16:22:00.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="拥抱Docker（一）常用命令">
<meta name="twitter:description" content="&amp;emsp;&amp;emsp;Docker是Docker.Inc开发的一款开源容器引擎，它在容器的基础上，将文件、网络、进程等一系列资源进行进一步封装和管理，通过对镜像的打包和封装，以及引入Docker Registry集中存储Docker镜像，从而简化了整个开发、测试和部署的环境和流程。">
<meta name="twitter:image" content="http://ww1.sinaimg.cn/large/639cfad5gy1g3erlk4ez1j20xk0eon4e.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/05/27/拥抱Docker（一）常用命令/"/>





  <title>拥抱Docker（一）常用命令 | 矩阵编程</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?7d29e6fba8e07d1a8af2414a20e5f2a6";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">矩阵编程</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-主页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-主页"></i> <br />
            
            主页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-分类"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-所有文章">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-文章"></i> <br />
            
            所有文章
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/05/27/拥抱Docker（一）常用命令/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="matrix">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="矩阵编程">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">拥抱Docker（一）常用命令</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-27T19:09:04+08:00">
                2019-05-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Docker/" itemprop="url" rel="index">
                    <span itemprop="name">Docker</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/05/27/拥抱Docker（一）常用命令/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/05/27/拥抱Docker（一）常用命令/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&emsp;&emsp;Docker是Docker.Inc开发的一款开源容器引擎，它在容器的基础上，将文件、网络、进程等一系列资源进行进一步封装和管理，通过对镜像的打包和封装，以及引入Docker Registry集中存储Docker镜像，从而简化了整个开发、测试和部署的环境和流程。<br><a id="more"></a></p>
<h4 id="镜像"><a href="#镜像" class="headerlink" title="镜像"></a>镜像</h4><h5 id="查找仓库镜像"><a href="#查找仓库镜像" class="headerlink" title="查找仓库镜像"></a>查找仓库镜像</h5><blockquote>
<p><strong>docker search [镜像仓库]</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker search nginx</span><br><span class="line">NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED</span><br><span class="line">nginx                             Official build of Nginx.                        <span class="number">11463</span>               [OK]</span><br><span class="line">jwilder/nginx-proxy               Automated Nginx reverse proxy <span class="keyword">for</span> docker con…   <span class="number">1602</span>                                    [OK]</span><br><span class="line">richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   <span class="number">715</span>                                     [OK]</span><br><span class="line">bitnami/nginx                     Bitnami nginx Docker Image                      <span class="number">66</span>                                      [OK]</span><br><span class="line">linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   <span class="number">62</span></span><br><span class="line">tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   <span class="number">45</span>                                      [OK]</span><br></pre></td></tr></table></figure></p>
</blockquote>
<p>上面search命令搜索所有镜像仓库中包含nginx的镜像，对应NAME格式用户名/镜像仓库，一般官方仓库仅有仓库名，比如上面的第一个镜像仓库是官方nginx仓库。</p>
<h5 id="获取仓库镜像"><a href="#获取仓库镜像" class="headerlink" title="获取仓库镜像"></a>获取仓库镜像</h5><blockquote>
<p><strong>docker pull [仓库]:[tag]</strong></p>
</blockquote>
<p>可以拉取指定标签的镜像，如果没有tag，默认拉取最新(latest)的镜像：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker pull nginx</span><br><span class="line">Using <span class="keyword">default</span> tag: latest</span><br><span class="line">latest: Pulling from library/nginx</span><br><span class="line"><span class="number">743f</span>2d6c1f65: Pull complete</span><br><span class="line"><span class="number">6</span>bfc4ec4420a: Pull complete</span><br><span class="line"><span class="number">688</span>a776db95f: Pull complete</span><br><span class="line">Digest: sha256:<span class="number">23</span>b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68</span><br><span class="line">Status: Downloaded newer image <span class="keyword">for</span> nginx:latest</span><br></pre></td></tr></table></figure></p>
<h5 id="查看本地镜像"><a href="#查看本地镜像" class="headerlink" title="查看本地镜像"></a>查看本地镜像</h5><blockquote>
<p><strong>docker images</strong></p>
</blockquote>
<p>可以看到刚刚下载的nginx镜像：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE</span><br><span class="line">nginx                                      latest              <span class="number">53f</span>3fd8007f7        <span class="number">2</span> weeks ago         <span class="number">109</span>MB</span><br></pre></td></tr></table></figure>
<h5 id="删除本地镜像"><a href="#删除本地镜像" class="headerlink" title="删除本地镜像"></a>删除本地镜像</h5><blockquote>
<p><strong>docker rmi [镜像]</strong></p>
</blockquote>
<p>可以来看下使用这个命令删除刚才拉取的镜像：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker rmi nginx</span><br><span class="line">Error response from daemon: conflict: unable to remove repository reference <span class="string">"nginx"</span> (must force) - container <span class="number">567831</span>b5df74 is using its referenced image <span class="number">53f</span>3fd8007f7</span><br></pre></td></tr></table></figure>
<p>报了一个错误，因为我之前运行过这个镜像，不过上面说可以强制删除，我们加一个-f参数再来试一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker rmi -f nginx</span><br><span class="line">Untagged: nginx:latest</span><br><span class="line">Untagged: nginx<span class="meta">@sha</span>256:<span class="number">23</span>b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68</span><br><span class="line">Deleted: sha256:<span class="number">53f</span>3fd8007f76bd23bf663ad5f5009c8941f63828ae458cef584b5f85dc0a7bf</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker images</span><br><span class="line">REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE</span><br></pre></td></tr></table></figure>
<p>成功删除了这个镜像。</p>
<h4 id="容器"><a href="#容器" class="headerlink" title="容器"></a>容器</h4><h5 id="创建、运行"><a href="#创建、运行" class="headerlink" title="创建、运行"></a>创建、运行</h5><p>容器创建并运行的命令比较复杂，这里只简单地讨论：</p>
<blockquote>
<p><strong>docker run [参数] [镜像]</strong></p>
</blockquote>
<p>重新下载删除的nginx镜像，执行以下命令运行：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker pull nginx</span><br><span class="line">Using <span class="keyword">default</span> tag: latest</span><br><span class="line">latest: Pulling from library/nginx</span><br><span class="line"><span class="number">743f</span>2d6c1f65: Already exists</span><br><span class="line"><span class="number">6</span>bfc4ec4420a: Already exists</span><br><span class="line"><span class="number">688</span>a776db95f: Already exists</span><br><span class="line">Digest: sha256:<span class="number">23</span>b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68</span><br><span class="line">Status: Downloaded newer image <span class="keyword">for</span> nginx:latest</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker run -p <span class="number">80</span>:<span class="number">80</span> -d nginx</span><br><span class="line"><span class="number">7</span>c878efcb65984952caeb83f6c81b69b6ebcc9228a15df7eec9ab25076819535</span><br></pre></td></tr></table></figure>
<p>现在nginx已经成功运行了，在浏览器上输入localhost查看：</p>
<p><img src="http://ww1.sinaimg.cn/large/639cfad5gy1g3erlk4ez1j20xk0eon4e.jpg" alt></p>
<p>这里列举几个常用的的参数简单说明：</p>
<ul>
<li>-p: 指定端口映射，宿主机端口:容器端口</li>
<li>-d: 以后台的形式运行容器</li>
<li>-t: 给容器分配一个伪终端，通常与-i配合使用</li>
<li>-i: 以交互的形式运行，与-t配合使用</li>
</ul>
<h5 id="查看"><a href="#查看" class="headerlink" title="查看"></a>查看</h5><p>列出所有运行中的容器：</p>
<blockquote>
<p><strong>docker ps [参数]</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES</span><br><span class="line"><span class="number">7</span>c878efcb659        nginx               <span class="string">"nginx -g 'daemon of…"</span>   <span class="number">19</span> hours ago        Up <span class="number">19</span> hours         <span class="number">0.0</span>.0.0:<span class="number">80</span>-&gt;<span class="number">80</span>/tcp   festive_keller</span><br></pre></td></tr></table></figure>
<p>列举几个常用的参数：</p>
<ul>
<li>-a: 显示所有的容器，包括未运行的</li>
<li>–format :指定返回的模板</li>
</ul>
<p>查看容器详情：</p>
<blockquote>
<p><strong>docker inspect[容器]</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES</span><br><span class="line"><span class="number">7</span>c878efcb659        nginx               <span class="string">"nginx -g 'daemon of…"</span>   <span class="number">26</span> hours ago        Up <span class="number">2</span> seconds        <span class="number">0.0</span>.0.0:<span class="number">80</span>-&gt;<span class="number">80</span>/tcp   festive_keller</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker inspect <span class="number">7</span>c878efcb659</span><br><span class="line"><span class="comment">// 内容太多只保留了一部分</span></span><br><span class="line">[</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="string">"Id"</span>: <span class="string">"7c878efcb65984952caeb83f6c81b69b6ebcc9228a15df7eec9ab25076819535"</span>,</span><br><span class="line">        <span class="string">"Created"</span>: <span class="string">"2019-05-26T07:43:48.0239342Z"</span>,</span><br><span class="line">        <span class="string">"Path"</span>: <span class="string">"nginx"</span>,</span><br><span class="line">        <span class="string">"Args"</span>: [</span><br><span class="line">            <span class="string">"-g"</span>,</span><br><span class="line">            <span class="string">"daemon off;"</span></span><br><span class="line">        ],</span><br><span class="line">        <span class="string">"State"</span>: &#123;</span><br><span class="line">            <span class="string">"Status"</span>: <span class="string">"running"</span>,</span><br><span class="line">            <span class="string">"Running"</span>: <span class="keyword">true</span>,</span><br><span class="line">            <span class="string">"Paused"</span>: <span class="keyword">false</span>,</span><br><span class="line">            <span class="string">"Restarting"</span>: <span class="keyword">false</span>,</span><br><span class="line">            <span class="string">"OOMKilled"</span>: <span class="keyword">false</span>,</span><br><span class="line">            <span class="string">"Dead"</span>: <span class="keyword">false</span>,</span><br><span class="line">            <span class="string">"Pid"</span>: <span class="number">11786</span>,</span><br><span class="line">            <span class="string">"ExitCode"</span>: <span class="number">0</span>,</span><br><span class="line">            <span class="string">"Error"</span>: <span class="string">""</span>,</span><br><span class="line">            <span class="string">"StartedAt"</span>: <span class="string">"2019-05-27T09:19:11.6796098Z"</span>,</span><br><span class="line">            <span class="string">"FinishedAt"</span>: <span class="string">"2019-05-27T09:15:42.5193429Z"</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h5 id="停止、启动和重启容器"><a href="#停止、启动和重启容器" class="headerlink" title="停止、启动和重启容器"></a>停止、启动和重启容器</h5><blockquote>
<p><strong>docker stop [容器]</strong><br><strong>docker start [容器]</strong><br><strong>docker restart [容器]</strong></p>
</blockquote>
<p>看一下容器停止：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES</span><br><span class="line">7c878efcb659        nginx               &quot;nginx -g &apos;daemon of…&quot;   26 hours ago        Up 23 seconds       0.0.0.0:80-&gt;80/tcp   festive_keller</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker stop 7c878efcb659</span><br><span class="line">7c878efcb659</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES</span><br></pre></td></tr></table></figure></p>
<h5 id="执行命令"><a href="#执行命令" class="headerlink" title="执行命令"></a>执行命令</h5><blockquote>
<p>docker exec [参数] [容器] [命令]</p>
</blockquote>
<p>比如我想进入nginx容器看一下文件列表：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps -a</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES</span><br><span class="line">e1be7b04269f        nginx               <span class="string">"nginx -g 'daemon of…"</span>   <span class="number">17</span> <span class="function">seconds ago      <span class="title">Exited</span> <span class="params">(<span class="number">0</span>)</span> 4 seconds ago                       laughing_davinci</span></span><br><span class="line"><span class="function">luyundeMacBook-Pro:~ luyun$ docker exec -it e1be7b04269f  /bin/bash</span></span><br><span class="line"><span class="function">Error response from daemon: Container e1be7b04269f17ae32c8a14c9a3f2f62f8cc58fa6be4887a05a8762c109f20cd is not running</span></span><br></pre></td></tr></table></figure></p>
<p>报错了，exec只能进入运行中的容器执行命令，所以先启动nginx：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES</span><br><span class="line">e1be7b04269f        nginx               <span class="string">"nginx -g 'daemon of…"</span>   <span class="number">5</span> minutes ago       Up <span class="number">3</span> minutes        <span class="number">0.0</span>.0.0:<span class="number">80</span>-&gt;<span class="number">80</span>/tcp   laughing_davinci</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker exec -it e1be7b04269f /bin/bash</span><br><span class="line">root@e1be7b04269f:/# ls</span><br><span class="line">bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  <span class="keyword">var</span></span><br></pre></td></tr></table></figure></p>
<h5 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h5><blockquote>
<p>docker rm [参数] [容器]</p>
</blockquote>
<p>尝试删除运行中的nginx容器：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES</span><br><span class="line"><span class="number">7</span>c878efcb659        nginx               <span class="string">"nginx -g 'daemon of…"</span>   <span class="number">26</span> hours ago        Up <span class="number">3</span> minutes        <span class="number">0.0</span>.0.0:<span class="number">80</span>-&gt;<span class="number">80</span>/tcp   festive_keller</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker rm <span class="number">7</span>c878efcb659</span><br><span class="line">Error response from daemon: You cannot remove a running container <span class="number">7</span>c878efcb65984952caeb83f6c81b69b6ebcc9228a15df7eec9ab25076819535. Stop the container before attempting removal or force remove</span><br></pre></td></tr></table></figure></p>
<p>报错了，不能删除正在运行的容器，先停止容器或者添加-f参数强制删除：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES</span><br><span class="line"><span class="number">7</span>c878efcb659        nginx               <span class="string">"nginx -g 'daemon of…"</span>   <span class="number">26</span> hours ago        Up <span class="number">5</span> minutes        <span class="number">0.0</span>.0.0:<span class="number">80</span>-&gt;<span class="number">80</span>/tcp   festive_keller</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker rm -f <span class="number">7</span>c878efcb659</span><br><span class="line"><span class="number">7</span>c878efcb659</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Docker/" rel="tag"># Docker</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/04/04/谈谈Spring的IoC之实例化/" rel="next" title="谈谈Spring的IoC之实例化">
                <i class="fa fa-chevron-left"></i> 谈谈Spring的IoC之实例化
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/06/04/拥抱Docker（二）Dockerfile制作镜像/" rel="prev" title="拥抱Docker（二）Dockerfile制作镜像">
                拥抱Docker（二）Dockerfile制作镜像 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">matrix</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">47</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#镜像"><span class="nav-number">1.</span> <span class="nav-text">镜像</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#查找仓库镜像"><span class="nav-number">1.1.</span> <span class="nav-text">查找仓库镜像</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#获取仓库镜像"><span class="nav-number">1.2.</span> <span class="nav-text">获取仓库镜像</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#查看本地镜像"><span class="nav-number">1.3.</span> <span class="nav-text">查看本地镜像</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#删除本地镜像"><span class="nav-number">1.4.</span> <span class="nav-text">删除本地镜像</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#容器"><span class="nav-number">2.</span> <span class="nav-text">容器</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#创建、运行"><span class="nav-number">2.1.</span> <span class="nav-text">创建、运行</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#查看"><span class="nav-number">2.2.</span> <span class="nav-text">查看</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#停止、启动和重启容器"><span class="nav-number">2.3.</span> <span class="nav-text">停止、启动和重启容器</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#执行命令"><span class="nav-number">2.4.</span> <span class="nav-text">执行命令</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#删除"><span class="nav-number">2.5.</span> <span class="nav-text">删除</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">matrix</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>
<div class=BbeiAn-info">
  浙ICP备 -
  <a href="http://www.beian.miit.gov.cn">18040498号</a>
  </a>
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://luyun.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://yoursite.com/2019/05/27/拥抱Docker（一）常用命令/';
          this.page.identifier = '2019/05/27/拥抱Docker（一）常用命令/';
          this.page.title = '拥抱Docker（一）常用命令';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://luyun.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
